Ext.onReady(function(){
	MODEL.initPage();
});
var MODEL = {
		initPage: function(){
			Ext.create('Ext.container.Viewport', {
			    layout: 'border',
			    frame:false,
			    border:false,
			    items: [{
			        region: 'west',
			        layout: 'fit',
			        items:[MODEL.createModelTree()],
			        width: Utils.getWidth()*0.22
			    },{
			    	region: 'center',
			    	layout: 'fit',
			    	items:[MODEL.createRoleGrid()]
			    }]
			});
		},
		/**
		 * 创建用户系统风格
		 */
		createModelTree: function(){
			var tree = Ext.create("Ext.com.bkfm.tree",{
				title: '业务模型',
		 		id: "modelTree",
		 		rootVisible: true,
		 		rootLabel: "业务模型",
		 		treeConfigId: "modelTree",
		 		rootTreeId: "00000000000000000000000000000001",
		 		border: false,
		 		autoScroll: true,
		 		tbar:['业务模型：',{
		 			xtype: 'textfield',
		 			id: 'model_name'
		 		},{
		 			text: '查询',
		 			iconCls: 'search',
		 			handler: function(){
		 				var modelName = Ext.getCmp("model_name").getValue();
		 				/*if(Ext.isEmpty(modelName)){
		 					AjaxUtils.showError("请输入查询条件");
		 					return false;
		 				}*/
		 				//var loader = tree.getLoader(); 
		 				//loader.load(tree.root);  
		 				tree.filterByText(modelName);
		 			}
		 		}]
			});
			tree.on("itemclick",function(view,record,item,index,e,opts){
				var rolegrid = Ext.getCmp("rolegrid");
		 		var id = record.get("id");
		 		if(id == tree.rootTreeId){
		 			rolegrid.setTitle("角色");
		 			rolegrid.model_path = "";
		 			rolegrid.getSelectionModel().deselectAll();
		 			return;
		 		}
		 		var title = record.get("text");
		 		var isleaf = record.get("leaf");
		 		var obj_path = record.raw.obj_path;
		 		rolegrid.getSelectionModel().deselectAll();
		 		BkfmDBUtils.Query("SELECT DISTINCT ROLE_RWID,BTN_PERMIS_RWID,OBJ_PATH FROM BLK_MODEL_ROLE_PERMIS WHERE OBJ_PATH='"+obj_path+"'",function(result){
		 			if(result.status == 'OK'){
		 				if(result.status == 'OK'){
							var records = result.list;
							var sel_records = [];
							for(i in records){
								var role_id = records[i].ROLE_RWID;
								rolegrid.selectRecordById(role_id)
							}
						}
		 			}
		 		});
		 		rolegrid.setTitle(title+"-角色");
		 		rolegrid.model_path = obj_path;
		 		
			});
			return tree;
		},
		/**
		 * 角色
		 */
		createRoleGrid: function(){
			var rolegrid = Ext.create('Ext.com.bkfm.datagrid', {
				title: "角色",
				id: 'rolegrid',
				objPath : "ROOT/XTGLPT/JCSJGL/JSXX/BLK_BASE_ROLES",
				orderby : ' ORDER BY ORDER_BY ASC',
				border: true,
				showstandardBtn: false,
				checkBoxOnly: true,
				model_path: '',//选择的业务模型路径
				userConfigColumn:[{
					index: 50,
					text: '查看功能权限',
					width: 150,
					renderer: function(value, cellmeta, record, rowIndex, columnIndex, store){
						var role_id = record.getId();
						var role_name = record.get("ROLE_NAME");
						return '<button onclick="javascript:MODEL.viewModeBtn(\''+role_id+'\',\''+role_name+'\');">查看权限</button>';
					}
				}],
				listeners:{
					cellclick: function(){
						//debugger;
					}
				}
			});
			return rolegrid;
		},
		/**
		 * 查看角色对应功能权限
		 * @param role_id角色编码
		 * @param role_name 角色名称
		 */
		viewModeBtn: function(role_id,role_name){
			var rolegrid = Ext.getCmp("rolegrid");
			var tree = Ext.getCmp("modelTree");
			var treeNode = tree.getSelectNode();
			if(treeNode==null || treeNode.getId()==tree.rootTreeId || rolegrid.model_path==""){
				AjaxUtils.showError("请选择业务模型");
				return ;
			}
			var win = Ext.create('Ext.com.bkfm.window', {
				title : "功能权限-【"+role_name+"】",
				id: 'modelBtnWin',
				autoScroll : true,
				modal : true,
				height : Utils.getHeight()*0.8,
				width : Utils.getWidth()*0.7,
				items:[MODEL.createBtnGrid()],
				listeners:{
					show: function(){
						var btnGrid = Ext.getCmp("btnGrid");
						btnGrid.role_id = role_id;
						btnGrid.role_name = role_name;
						btnGrid.model_path = rolegrid.model_path;
					}
				}
			}).show();
		},
		/**
		 * 功能
		 */
		createBtnGrid: function(){
			var btnGrid = Ext.create('Ext.com.bkfm.datagrid', {
				id: "btnGrid",
				objPath : "ROOT/XTGLPT/JCSJGL/QXGL/BLK_BTN_PERMIS",
				border: true,
				orderby : ' ORDER BY ORDER_BY ASC',
				hiddenField:['ORDER_BY'],
				showstandardBtn: false,
				tbar:[{
					text: '保存',
					iconCls: 'save',
					handler: function(){
						var tree = Ext.getCmp("modelTree");
						var treeNode = tree.getSelectNode();
						if(treeNode==null || treeNode.getId()==tree.rootTreeId){
							AjaxUtils.showError("请选择业务模型");
							return ;
						}
						
						var role_id = btnGrid.role_id;
						var role_name = btnGrid.role_name;
						var model_path = btnGrid.model_path;
						//保存
						var btn_records = btnGrid.getSelectedRecords();
						var param = [];
						for(i in btn_records){
							var btn_r = btn_records[i];
							param.push({
								"obj_path": "ROOT/XTGLPT/JCSJGL/QXGL/BLK_MODEL_ROLE_PERMIS",
								"record_state": Constants.RECORD_NEW,
								"rwid": Utils.getRwid(),
								"record_data":Ext.JSON.encode({
									"role_rwid": role_id,
									"BTN_PERMIS_RWID": btn_r.getId(),
									"obj_path": model_path
								})
							});
						}
						//先删除
						BkfmDBUtils.Execute("DELETE FROM BLK_MODEL_ROLE_PERMIS WHERE OBJ_PATH='"+model_path+"' AND ROLE_RWID='"+role_id+"'",function(result){
							if(result.status=="OK"){
								AjaxUtils.CallService({
									url: CONTEXT.contextPath+"/bkfm/metadata/v1/savemodelmetadata",
									dataType: "json",
									params: param,
									call: function(data){
										if(data.status == "OK"){
											AjaxUtils.showInfo('操作成功',function(){
												Ext.getCmp("modelBtnWin").close();
											});
										}else{
											AjaxUtils.showError('操作失败');
										}
									}
								});
							}
						});
					}
				}],
				afterload: function(store,records){
					var model_path = btnGrid.model_path;
					var role_id = btnGrid.role_id;
					if(model_path == ""){
						return;
					}
					//btnGrid.getSelectionModel().deselectAll();
					BkfmDBUtils.Query("SELECT ROLE_RWID,BTN_PERMIS_RWID,OBJ_PATH FROM BLK_MODEL_ROLE_PERMIS WHERE OBJ_PATH='"+model_path+"' AND ROLE_RWID='"+role_id+"'",function(result){
						if(result.status == 'OK'){
							var records = result.list;
							var sel_records = [];
							for(i in records){
								var btn_permis_rwid = records[i].BTN_PERMIS_RWID;
								btnGrid.selectRecordById(btn_permis_rwid)
							}
						}
					});
				}
			});
			
			return btnGrid;
		}/*,
		createModelGrid: function(){
			var grid = Ext.create('Ext.com.bkfm.datagrid', {
				title: "业务模块角色功能",
				id: 'modelGrid',
				objPath : "ROOT/XTGLPT/JCSJGL/QXGL/BLK_MODEL_ROLE_PERMIS",
				border: true,
				showstandardBtn: false,
				gridEditor: true,
				tbar:[{
					text: '保存',
					iconCls: 'save',
					handler: function(){
						grid.save();
					}
				},'->',{
					text: '新建',
					iconCls: 'add',
					handler: function(){
						grid.addNewRecord();
					}
				}]
			});
			return grid;
		}*/
}